package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.mapper.SetMealMapper;
import com.itheima.pojo.CheckGroupAndCheckItem;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetMealService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class SetMealServiceImpl implements SetMealService {
    private final SetMealMapper setMealMapper;
    @Override
    public void addSetMeal(Setmeal setmeal, List<Integer> ids) {
        setMealMapper.add(setmeal);
        Integer setmealId = setmeal.getId();
        for (Integer id : ids) {
            setMealMapper.insert(setmealId, id);
        }
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<Setmeal> page = setMealMapper.pageQuery(queryPageBean);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public Setmeal findByIdSetMeal(Integer id) {
        return setMealMapper.findById(id);
    }

    @Override
    public void updateSetMeal(Setmeal setmeal, List<Integer> ids) {
        setMealMapper.update(setmeal);
        setMealMapper.deleteById(setmeal.getId());
        for (Integer id : ids) {
            setMealMapper.insert(setmeal.getId(), id);
        }
    }
}
